import { statisticsTotal } from "/@/api/statistics/index.js";
let timerState = 0;

let whiteMetaTitle = ["页面正在刷新中...", "页面找不到"];
let manhourStatistics = (path) => {
  if (path == null) return;
  if (whiteMetaTitle.includes(path.meta.title)) return;
  let flag = false;

  let makeSetTimeOut = () => {
    clearTimeout(timerState);

    timerState = setTimeout(() => {
      makeSetTimeOut();
      flag ? sendHttp() : "";
    }, 60000);
  };
  makeSetTimeOut();
  document.addEventListener("click", () => (flag = true));
  document.addEventListener("mousemove", () => (flag = true));
  document.addEventListener("keydown", () => (flag = true));

  let sendHttp = async () => {
    flag = false;

    if (localStorage.getItem("userMessage")) {
      await statisticsTotal({
        sysName: "化工设备数字化设计工作平台", //系统名称
        moduleName: path.meta.title, //模块名称
        userName:
          JSON.parse(localStorage.getItem("userMessage"))?.user_name || "", //用户名
        accessDurationMin: 1, //停留时间（和访问次数二选一）
      });
    }
  };

  let sendPageMessage = async (path) => {
    if (localStorage.getItem("userMessage")) {
      await statisticsTotal({
        sysName: "化工设备数字化设计工作平台", //系统名称
        moduleName: path.meta.title, //模块名称
        userName:
          JSON.parse(localStorage.getItem("userMessage"))?.user_name || "", //用户名
        accessCount: 1, //停留时间（和访问次数二选一）
      });
    }
  };
  sendPageMessage(path);
};

export default manhourStatistics;
